/**
 * ----------------------------------------------------------------------------
 * Copyright Jens Konopka
 * 
 * Alle Rechte vorbehalten.
 * Licence: Apache 2.0
 * ----------------------------------------------------------------------------
 *
 * created on 02.03.2012 by jkonopka
 */
package de.sql.toolkit.manager;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * @author jkonopka
 * 
 */
public final class ConnectionManager {

	private static String driver;
	private static String url;
	private static String user = "";
	private static String password = "";

	private static Connection connection;
	private static Class<?> driverClass;

	/**
	 * @return the url
	 */
	public static String getUrl() {
		return url;
	}

	/**
	 * @param url the url to set
	 */
	public static void setUrl(String url) {
		ConnectionManager.url = url;
	}

	/**
	 * @throws SQLException
	 */
	private static void initDriverClass() throws SQLException {
		if (getDriverClass() == null) {
			try {
				setDriverClass(Class.forName(getDriver()));
			} catch (final ClassNotFoundException e) {
				throw new SQLException(e);
			}
		}
	}

	/**
	 * @return
	 * @throws SQLException
	 */
	public static Connection getConnection() throws SQLException {
		initDriverClass();
		if (connection == null || connection.isClosed()) {
			connection = DriverManager.getConnection(getUrl(), getUser(), getPassword());
		}
		return connection;
	}

	/**
	 * @return the driver
	 */
	public static String getDriver() {
		return driver;
	}

	/**
	 * @param driver the driver to set
	 */
	public static void setDriver(String driver) {
		ConnectionManager.driver = driver;
	}

	/**
	 * @return the user
	 */
	public static String getUser() {
		return user;
	}

	/**
	 * @param user the user to set
	 */
	public static void setUser(String user) {
		ConnectionManager.user = user;
	}

	/**
	 * @return the password
	 */
	public static String getPassword() {
		return password;
	}

	/**
	 * @param password the password to set
	 */
	public static void setPassword(String password) {
		ConnectionManager.password = password;
	}

	/**
	 * @param connection the connection to set
	 */
	public static void setConnection(Connection connection) {
		ConnectionManager.connection = connection;
	}

	/**
	 * @return the driverClass
	 */
	public static Class<?> getDriverClass() {
		return driverClass;
	}

	/**
	 * @param driverClass the driverClass to set
	 */
	public static void setDriverClass(Class<?> driverClass) {
		ConnectionManager.driverClass = driverClass;
	}

}
